package cheqex.update.controllers;

import cheqex.dao.AdminPeer;
import javax.swing.JOptionPane;
import cheqex.update.controllers.*;
public class AccountHandler{
	private String user; 
	private char pw[];
	
	public AccountHandler(String user, char pw[]){
		this.user = user; 
		this.pw = pw;
	}
/******************* CHEQEX MAIN - INSTALL ADMIN **********************/
	
	public static void installAdmin(){
		if(AdminPeer.installAdmin()){
			AdminPeer.addAdmin();
		}
	}
	
	
/*********************** LOG IN VIEW **********************************/

	//checks if username and password exists
	public static boolean AccountCheck(String user, char pw[]){
		String pass = new String(pw);
		if(AdminPeer.adminCheck(user,pass))
			return true;
		else return false;
	}

	//checks if the username entered is in the database
	public static boolean UserNameExists(String user){
		if(AdminPeer.adminUsername(user))
			return true;
		else return false;
	}

	
/*********************** CHANGE USER SETTINGS **********************************/
	

	//verifies if the username has 4-30 characters and is the same with the confirmation of username
	public static boolean VerifyUsername(String username, String confirmUsername){
		if((IsUsernameLengthOk(username))&&username.equals(confirmUsername)){
			try{
				if(AdminPeer.changeUsername(username)){
					if(ActivitiesController.addActivity(DateController.getToday(), "Updated username.")){
						JOptionPane.showMessageDialog(null, "Username has been changed.", "Successful", JOptionPane.DEFAULT_OPTION);
						return true;
					}else{
						JOptionPane.showMessageDialog(null, "An error happened.", "Error", JOptionPane.DEFAULT_OPTION);
						return false;
					}
				}else return false;
			}catch(Exception e){
				e.printStackTrace();
				return false;
			}
		}
		else{
			String errorMessage = getUsernameError(username,confirmUsername);
			JOptionPane.showMessageDialog(null, errorMessage, "Error", JOptionPane.WARNING_MESSAGE);
			return false;
		}
	}
	
	//returns error message
	public static String getUsernameError(String username, String confirmUsername){
		if(IsUsernameLengthOk(username)&&IsUsernameLengthOk(confirmUsername))
			return "Confirmation did not match";
		else{
			if (username.length()==0)
				return "You did not enter a username.";
			else if(confirmUsername.length()==0)
				return "You did not re-enter username for confirmation.";
			else if(username.equals(confirmUsername))
				return "Username either too long or too short.";
			else
				return "Confirmation did not match and username either too long or too short.";
		}
	}
	
	//checks if the length of the username is 4-30 characters long
	public static boolean IsUsernameLengthOk(String username){
		if(username.length()>=4&&username.length()<=30)
			return true;
		else
			return false;
	}
	
	//verifies if the password has 4-20 characters and is the same with the confirmation of password
	public static boolean VerifyPassword(char[] password, char[] confirmPassword){
		String pass = new String(password);
		String confirmPass = new String(confirmPassword);
		if(IsPasswordLengthOk(pass)&&pass.equals(confirmPass)){
			if(AdminPeer.changePassword(pass)){
				if(ActivitiesController.addActivity(DateController.getToday(), "Updated password.")){
					JOptionPane.showMessageDialog(null, "Password has been changed.", "Successful", JOptionPane.DEFAULT_OPTION);
					return true;
				}else{
					JOptionPane.showMessageDialog(null,"An error happened.","Error",JOptionPane.DEFAULT_OPTION);
					return false;
				}
			} else return false;
		}
		else{
			String errorMessage = getPasswordError(password,confirmPassword);
			JOptionPane.showMessageDialog(null, errorMessage, "Error", JOptionPane.WARNING_MESSAGE);
			return false;
		}
	}
	
	//returns error message
	public static String getPasswordError(char[] password, char[] confirmPassword){
		String pass = new String(password);
		String confirmPass = new String(confirmPassword);
		if(IsPasswordLengthOk(pass)&&IsPasswordLengthOk(confirmPass))
			return "Confirmation did not match";
		else{
			if (pass.length()==0)
				return "You did not enter a password.";
			else if(confirmPass.length()==0)
				return "You did not re-enter password for confirmation.";
			else if(pass.equals(confirmPass))
				return "Password either too long or too short.";
			else
				return "Confirmation did not match and password either too long or too short.";
		}
	}
	
	//checks if the length of the password is 4-20 characters long
	public static boolean IsPasswordLengthOk(String password){
		if(password.length()>=4&&password.length()<=20)
			return true;
		else
			return false;
	}
}
